/**
 * 
 */


function FloatWindowLogin () {

	var loginSuccessCallBack = function () {};
	
	var floatWindowLoginHtml = 
		
		"<div class=float-login>" +
		"   <div class=dialog>" +
		"	    <div class=operate>" +
		"		    <div class=close></div>" +
		"   	</div>" +
		"   	<div class=title>" +
		"   		<span class=login>系统登录</span>" +
		"	    </div>" +
		"	    <div class=content>" +
		"		    <p>用户名╱邮箱╱手机号</p>" +
		"		    <div class='user username'>" +
		"			    <input type=text     name=username autocomplete=off />" +
		"		    </div>" +
		"		    <div class='user password'>" +
		"		    	<input type=password name=password autocomplete=off />" +
		"	    	</div>" +
		"		    <div class=login>" +
		"		    	<button>登录</button>" +
		"		    	<!--<a href='#'>忘记密码？</a>-->" +
		"		    	<p></p>" +
		"		    </div>" +
		"		    <div class=others>" +
		"		    	<div class=_title>第三方账号直接登录</div>" +
		"		    	<div class='button wb'>微博账号登录</div>" +
		"		    	<div class='button qq'>QQ账号登录</div>" +
		"		    </div>" +
		"	    </div>" +
		"   </div>" + 
		"</div>";
	
	var element = {};
    
	
	
	function initFloatWindowLogin () {
		
		element.rect = {};
		
		element.rootElement = $(floatWindowLoginHtml);
		element.rootElement.appendTo("body");
		element.backgroundElement = $('<div class="float-login-background"></div>');
		element.backgroundElement.appendTo("body");
		element.dialogElement = element.rootElement.find(".dialog");
		
		element.rect.width  = element.rootElement.width();
		element.rect.height = element.rootElement.height();
		
		$(window).resize(onResizeBrowerWindow);
		$(".float-login .content .login button").click(onClickLoginButton);
		$(".float-login .operate .close").click(onClickCloseButton);
		
	}
	
	function onClickLoginButton(event) {
		
		var e = event || window.event || arguments.callee.caller.arguments[0];
	
		var errorMessageElement = $(e.target).parents(".login").find("p")[0];		
		var username = element.rootElement.find("input[name=username]").val();
		var password = element.rootElement.find("input[name=password]").val();
		if(username == "" || password == "") {
			errorMessageElement.innerHTML = "*账号╱密码不能为空";
			return;
		} else {
			errorMessageElement.innerHTML = "";
		}
	
		var request = { 'username' : username, 'password' : password };
	
		$.ajax({
			url: window.webService('/user/login'),
			data: request,
			success: function (object) {
				if(object.status == 0) {
					closeFloatWindowLoginDialog(function() {
						loginSuccessCallBack(object.content);
					});
				} else if(object.status == -1) {
					errorMessageElement.innerHTML = '*账号╱密码不正确';
				}
			}
		});
	}
	
	function onClickCloseButton (event) {
		closeFloatWindowLoginDialog();
	}
	
	function onResizeBrowerWindow () {
		adjustFloatWindowLoginDialog();
	}
	
	function calculateFloatWindowLoginDialogPosition() {
		var position = {}, windowRect = {};
		windowRect.width = $(window).width();
		windowRect.height = $(window).height();
		position.left = windowRect.width > element.rect.width ? (windowRect.width - element.rect.width) / 2 : 0;
		position.top = windowRect.height > element.rect.height ? (windowRect.height - element.rect.height) / 2 : 0;
		return position;
	}
	
	function adjustFloatWindowLoginDialog(position) {
		if (position == undefined || position == null) {
			position = calculateFloatWindowLoginDialogPosition();
		}
		element.rootElement.css({"top": position.top, "left": position.left});
	}
	
	
	function closeFloatWindowLoginDialog (callback) {
		element.rootElement.animate({"top": "-500px"}, 200, function() {
			element.rootElement.hide();
			element.backgroundElement.hide();
			if(typeof callback === 'function') {
				callback();
			}
			document.documentElement.style.overflow='auto';
		});
	}
	
	function openFloatWindowLoginDialog () {
		var position = calculateFloatWindowLoginDialogPosition();
		adjustFloatWindowLoginDialog(position);
		element.rootElement.css({"top": -element.rect.height});
		element.rootElement.show();
		element.backgroundElement.show();
		element.rootElement.animate({"top": position.top}, 200, function () {
			
		});
		document.documentElement.style.overflow='hidden';
	}
	
	this.login = function (callback) {
		openFloatWindowLoginDialog();
		this.setLoginSuccessCallBack(callback);
	}
	
	this.setLoginSuccessCallBack = function (callback) {
		if (typeof callback === "function") {
			loginSuccessCallBack = callback;
		}
	}
	
    initFloatWindowLogin();
}
